<?php
session_start();
require("../config.php");
require_once($CONF['root.dir']."database.php");

$error = "";
$msg = "";
$element = $_GET["act"];
$folder = $_GET["folder"];
$arrtype = array("JPG", "JPEG", "GIF", "DOC", "PDF", "RAR", "ZIP", "PNG");
if(($element!="") && ($_SESSION['AR_userLogin']!="")){
		$ftype = explode(".",$_FILES[$element]['name']);
		$ftype = trim(strtoupper($ftype[count($ftype)-1]));
		if(!empty($_FILES[$element]['error'])){
			if(($_FILES[$element]['error']=="1") || ($_FILES[$element]['error']=="2")){
				$error = "<br><b>Error :</b><br>Ukuran File ".strtoupper($element)." Terlalu Besar.";
			}else if($_FILES[$element]['error']=="3"){
				$error = "<br><b>Error :</b><br>File ".strtoupper($element)." Yang Ter-Upload Tidak Sempurna.";
			}else if($_FILES[$element]['error']=="4"){
				$error = "<br><b>Error :</b><br>File ".strtoupper($element)." Kosong Atau Belum Dipilih.";
			}else if($_FILES[$element]['error']=="6"){
				$error = "<br><b>Error :</b><br>Direktori Penyimpanan Sementara Tidak Ditemukan.";
			}else if($_FILES[$element]['error']=="7"){
				$error = "<br><b>Error :</b><br>File ".strtoupper($element)." Gagal Ter-Upload.";
			}else if($_FILES[$element]['error']=="8"){
				$error = "<br><b>Error :</b><br>Proses Upload File ".strtoupper($element)." Dibatalkan.";
			}else{
				$error = "<br><b>Error :</b><br>Pesan Error Tidak Ditemukan.";
			}
		}else if(empty($_FILES[$element]['tmp_name']) || ($_FILES[$element]['tmp_name']=='none')){
			$error = "ss<br><b>Error :</b><br>File ".strtoupper($element)." Gagal Ter-Upload.";
		}else if(!in_array($ftype, $arrtype)){
			$error = "<br><b>Error :</b><br>Tipe File ".strtoupper($element)." Salah.<br>Tipe File Yang Diterima : *.JPG, *.JPEG, *.GIF, *.DOC, *.PDF, *.RAR Dan *.ZIP *.PNG";
		}else{
			$filename = $CONF['upload.dir'].$folder.'/Jawaban_'.$_SESSION["AR_nama"].".$ftype";
			$imagename=str_replace($CONF['upload.dir'].$folder.'/',"",$filename);
			if(move_uploaded_file($_FILES[$element]['tmp_name'], $filename)){
				$rstemp = 1;
				if($rstemp==1){
					$msg = "<table><tr><td colspan='3'>Upload File ".strtoupper($element)." Berhasil.</td></tr>";
					$msg .= '<tr><td>Nama File</td><td>:</td><td>'.$_FILES[$element]['name']."</td></tr>";
					$msg .= '<tr><td>Tipe File</td><td>:</td><td>'.$_FILES[$element]['type']."</td></tr>";
					$msg .= '<tr><td>Ukuran File</td><td>:</td><td>'.@filesize($filename)."</td></tr></table>";
					$msg .= '<b>Mohon Tunggu hingga Admin selesai memeriksa jawaban anda. Klik menu Hasil Tes untuk melihat hasilnya.</b>';
					
					$msg .= "<input type=\"hidden\" name=\"Jawaban\" id=\"Jawaban\" value=\"".$imagename."\"/>";	
					
				
					$SQL="select id_pelamar,nama,tmp_lahir,tgl_lahir,alamat,telepon,jns_kelamin,status,pendidikan,jurusan,ipk 
						  from tbllamaran where id_pelamar='".$_SESSION["AR_ID_PELAMAR"]."'";
		  			$conn->connect();
					$val=$conn->query($SQL);
					$val->next();
					
					$SQ="INSERT INTO tbltes(id_pelamar, nama, tmp_lahir, tgl_lahir, file, status,tgl_upload) 
						  VALUES('".$val->get(0)."', '".$val->get(1)."', '".$val->get(2)."', '".$val->get(3)."', '".$imagename."','0',sysdate())";
					
					$exec=$conn->execute($SQ);
					$conn->disconnect();
					
				}else{
					$error = "<br><b>Error :</b><br>File ".strtoupper($element)." Gagal Ter-Upload.";
					@unlink($filename);
				}
			}else{
				$error = "<br><b>Error :</b><br>File ".strtoupper($element)." Gagal Ter-Upload.";
			}
			@unlink($_FILES[$element]);
		}
}else{
	$error = "<br><b>Error :</b><br>Parameter Tidak Ditemukan.";
}
echo "{error: '$error',\n msg: '$msg'\n}";
?>